package com.walter.onomastici;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlParser {
	
	public Document getDocument(InputStream inputStream) throws SAXException, ParserConfigurationException{
		Document document = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try{
			DocumentBuilder db = factory.newDocumentBuilder();
			InputSource inputSource = new InputSource(inputStream);
			document = db.parse(inputSource);
		}catch(IOException e){e.printStackTrace();}
		return document;
	}
	
	public String getValue(Element item,String name){
		NodeList nodes = item.getElementsByTagName(name);
		return this.getTextNodeValue(nodes.item(0));
	}
	
	private final String getTextNodeValue(Node node){
		Node child;
		if(node != null){
			if(node.hasChildNodes()){
				child = node.getFirstChild();
				while(child != null){
					if(child.getNodeType() == Node.TEXT_NODE){
						return child.getNodeValue();
					}
					child.getNextSibling();
				}
			}
		}
		return "";
	}

}
